package woshiheima6;

import java.util.Comparator;
import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args) {
        //创建treeset集合对象
        TreeSet<Student> tr = new TreeSet<Student>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {

               int num =  s1.getAge() - s2.getAge();
              int num2= num == 0 ? s1.getName().compareTo(s2.getName()):num;
                return num2;
            }
        });

        Student s1 = new Student("zhangsan",19);
        Student s2 = new Student("lisi",18);
        Student s3 = new Student("wanger",18);
        Student s4 = new Student("bazi",20);

        tr.add(s1);
        tr.add(s2);
        tr.add(s3);
        tr.add(s4);

        for (Student s:tr){

            System.out.println(s.getName()+","+s.getAge());
        }
    }
}
